<template>
  <div class="container">
    <!-- 导航栏 -->
    <nav class="navbar">
      <div class="brand" @click="playAudio">
        <img src="./assets/logo.png" class="logo" alt="威龙图书">
        <span class="tooltip">威龙数字图书馆
            <span class="tooltip-text">掉皮掉肉不掉队！</span>
        </span>
      </div>
      
      <ul class="nav-links">
        <li>
          <router-link to="/" exact-active-class="active" class="nav-link">
            <i class="icon-home"></i>
            <span>首页</span>
          </router-link>
        </li>
        <li>
          <router-link to="/browse" active-class="active" class="nav-link">
            <i class="icon-search"></i>
            <span>图书检索</span>
          </router-link>
        </li>

        
        <!-- 个人中心导航项，只有登录后才显示 -->
        <li v-if="isLoggedIn">
          <router-link to="/personal_homepage" active-class="active" class="nav-link">
            <i class="icon-user"></i>
            <span>个人中心</span>
          </router-link>
        </li>

        <!-- <li>
          <router-link to="/personal_homepage" active-class="active" class="nav-link">
            <i class="icon-user"></i>
            <span>个人中心</span>
          </router-link>
        </li> -->

        <!-- <li>
          <router-link to="/login" active-class="active" class="nav-link login-btn">
            <i class="icon-login"></i>
            <span>登录注册</span>
          </router-link>
        </li> -->
        <li v-if="!isLoggedIn">  <!-- 添加条件判断 -->
          <router-link to="/login" active-class="active" class="nav-link login-btn">
            <i class="icon-login"></i>
            <span>登录注册</span>
          </router-link>
        </li>
      </ul>
    </nav>

    <!-- 页面内容 -->
    <main class="main-content">
      <router-view></router-view>
    </main>
  </div>
</template>

<script>
export default {
  name: 'AppLayout',
  data() {
    return {
      audio: null,
      isLoggedIn: false
    }
  },
  created() {
    // 初始化时检查本地存储
    const userData = localStorage.getItem('user')
    if (userData) {
      this.isLoggedIn = true
    }

    // 注册全局事件监听
    this.$emitter.on('user-login', () => {
      this.isLoggedIn = true
    })
    this.$emitter.on('user-logout', () => {
      this.isLoggedIn = false
    })
  },

  methods: {
    playAudio() {
      // 如果音频已经存在，则暂停并销毁
      if (this.audio) {
        this.audio.pause()
        this.audio = null
      }
      
      // 创建新的音频对象
      this.audio = new Audio(require('./assets/dpdrbdd.mp3'))
      
      this.audio.addEventListener('canplaythrough', () => {
        // 音频加载完成后播放
        this.audio.play()
          .catch(error => {
            console.error('播放音频失败:', error)
          })
      })
      
      // 监听音频播放完成事件
      this.audio.addEventListener('ended', () => {
        this.audio = null
      })
    }
  },
  beforeUnmount() {
    // 组件销毁时释放音频资源
    if (this.audio) {
      this.audio.pause()
      this.audio = null
    }
  }
}
</script>

<style scoped>
:root {
  --primary-color: #2c3e50;
  --secondary-color: #42b983;
  --accent-color: #f5a623;
  --text-light: #f8f9fa;
  --shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* 导航栏美化 */
.navbar {
  background: linear-gradient(135deg, var(--primary-color) 0%, #1a2836 100%);
  padding: 0 2rem;
  box-shadow: var(--shadow);
  position: relative;
  z-index: 1000;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.brand {
  display: flex;
  align-items: center;
  gap: 1rem;
  text-decoration: none;
  transition: transform 0.3s ease;
  cursor: pointer; /* 添加鼠标悬停样式 */
}

.brand:hover {
  transform: translateX(5px);
}

.logo {
  height: 40px;
  width: auto;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.2));
}

.brand span {
  color: var(--text-light);
  font-size: 1.4rem;
  font-weight: 600;
  letter-spacing: 0.5px;
}

/* 导航链接美化 */
.nav-links {
  display: flex;
  gap: 1.5rem;
  margin: 0;
  padding: 0;
  list-style: none;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--text-light);
  padding: 1rem 1.5rem;
  border-radius: 8px;
  text-decoration: none;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
}

.nav-link::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 0;
  height: 3px;
  background: var(--secondary-color);
  transition: width 0.3s ease;
}

.nav-link:hover {
  background: rgba(255, 255, 255, 0.05);
  transform: translateY(-2px);
}

.nav-link:hover::after {
  width: 100%;
}

.nav-link.active {
  background: linear-gradient(45deg, var(--secondary-color), #3aa373);
  box-shadow: var(--shadow);
}

.nav-link.active::after {
  display: none;
}

/* 图标样式 */
[class^="icon-"] {
  font-size: 1.2rem;
  transition: transform 0.3s ease;
}

.nav-link:hover [class^="icon-"] {
  transform: scale(1.1);
}

/* 登录按钮特殊样式 */
.login-btn {
  background: rgba(255, 255, 255, 0.1);
  border: 2px solid var(--secondary-color);
}

.login-btn:hover {
  background: var(--secondary-color);
}
.tooltip {
  position: relative;
  display: inline-block;
}

.tooltip .tooltip-text {
  visibility: hidden;
  width: 200px;
  background-color: #555;
  color: #fff;
  text-align: center;
  border-radius: 6px;
  padding: 5px;
  position: absolute;
  z-index: 1;
  bottom: -150%; /* 调整这个值来控制提示框的位置 */
  left: 50%;
  transform: translateX(-50%);
  opacity: 0;
  transition: opacity 0.3s;
}

.tooltip:hover .tooltip-text {
  visibility: visible;
  opacity: 1;
}
/* 主内容区 */
.main-content {
  flex: 1;
  padding: 2rem;
  background: linear-gradient(45deg, #f8f9fa, #e9ecef);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .navbar {
    flex-direction: column;
    padding: 1rem;
  }

  .nav-links {
    width: 100%;
    justify-content: center;
    margin-top: 1rem;
    flex-wrap: wrap;
  }

  .nav-link {
    padding: 0.8rem 1rem;
    font-size: 0.9rem;
  }

  .brand span {
    font-size: 1.2rem;
  }
}

@media (max-width: 480px) {
  .nav-links {
    gap: 0.5rem;
  }

  .nav-link {
    padding: 0.6rem 0.8rem;
  }

  .brand span {
    display: none;
  }
}
</style>